<template>
  <div class="search">
    <div class="search_header">
      <!-- <i class="iconfont">&#xe62f;</i> -->
      <!-- <div class="search_in">
              <i class="iconfont">&#xe61f;</i>
              <input type="text" placeholder="苹果">
          </div> -->
      <!-- <span>搜索</span> -->
      <van-icon name="arrow-left" color="#1ABC9c" size="20px"/>
      <van-search
        v-model="value"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
      >
        <template #action>
          <div @click="onSearch">搜索</div>
        </template>
      </van-search>
    </div>
    <div class="search_main">
      <ul v-if="search.length">
        <li v-for="item in search" :key="item.id" @click="to_detail(item.id)">
          <img :src="item.image" alt="" />
          <p>{{item.title}}</p>
          <span>￥ {{item.price}}</span> <b>￥{{item.originPrice}}</b>
        </li>
      </ul>
        <div class="nocontent" v-else>
          暂未搜索到数据
        </div>
    </div>
  </div>
</template>

<script>
import { getSearch } from '@/api/search'
export default {
  name: 'search',
  data() {
    return {
      value: '',
      search: [],
    }
  },
  methods: {
    async getSear() {
      const { list: search } = await getSearch(this.value)
      this.search = search
    },
    to_detail(data) {
      this.$router.push({ path: '/detail', query: { id: data } })
    },
    onSearch() {
      this.getSear()
    },
    onCancel() {
    //   Toast('取消')
    },
  },
}
</script>

<style lang="scss" scoped>
.search {
  width: 100%;
  height: 100%;
  background-color: #f9f9f9;
  .search_header {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    border-bottom: 1px solid #e4e4e4;
    .van-icon{
      margin-left: 10px;
    }
    .van-search, .van-search--show-action{
      background-color: #f9f9f9;
      height: 48px;
      .van-cell, .van-cell--borderless, .van-field{
        width: 240px;
        background-color: white;
        border: 1px solid #dedede;
        .van-field__control{
          width: 174px;
        }
      }
    }
    .van-search__action{
      width: 65px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .search_main {
    width: 100%;
    padding: 0 16px;
    margin-top: 10px;
    box-sizing: border-box;
    .nocontent{
      width: 100%;
      height: 100%;
      margin-top: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    ul {
      width: 100%;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      li {
        width: 160px;
        height: 210px;
        margin-bottom: 30px;
        img {
          width: 160px;
          height: 160px;
        }
        p {
          font-size: 12px;
          color: #666666;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          margin: 8px 0;
        }
        span {
          color: #f04844;
          font-size: 12px;
        }
        b {
          color: #999999;
          font-size: 12px;
          font-weight: 300;
          text-decoration: line-through;
          margin-left: 8px;
        }
      }
    }
  }
}
</style>
